package redsync

import (
	"fmt"
	"github.com/bwmarrin/snowflake"
	"os"
	"time"

	"github.com/samuel/go-zookeeper/zk"
)

func TestZK() {
	c, _, err := zk.Connect([]string{"127.0.0.1:8897"}, time.Second) //*10)
	if err != nil {
		panic(err)
	}


	fmt.Println("OK")

	defer c.Close()
	//l := zk.NewLock(c, "/lock", zk.WorldACL(zk.PermAll))
	//err = l.Lock()
	//if err != nil {
	//	panic(err)
	//}
	//println("lock succ, do your business logic")
	//
	//time.Sleep(time.Second * 10)
	//
	//// do some thing
	//l.Unlock()
	//println("unlock succ, finish business logic")
}

func UnqID() map[int]int{
	n, err := snowflake.NewNode(1)
	if err != nil {
		println(err)
		os.Exit(1)
	}

	cha:=make([] chan int,30000)

	list:=make(map[int]int)

	for i := 0; i < 30000; i++ {
		cha[i] = make(chan int)
		go Count(cha[i],n)

		//fmt.Println("node: ", id.Node(), "step: ", id.Step(), "time: ", id.Time(), "\n")
	}
	//for i:=0; i<10; i++ {
		//chs[i] = make(chan int)
		//go Count(chs[i])
	//}


	fmt.Println("分割")
	for _, ch := range(cha) {
		num:=<-ch
		list[num]=num

	}

	return list

}
func Count(ch chan int,node *snowflake.Node) {

	id:=node.Generate()
	Intid:=int(id)
	ch <-Intid
	//fmt.Println("Counting")
}
